package com.jp.fm.data;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.jp.fm.HomeFinanceRunner;

public class Institution implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String name = null;
	private Integer id = null;
	public static final String CACHE_NAME = "Institution";

	public Institution(final Integer id, final String name) {
		super();
		this.name = name;
		this.id = id;
	}

	public static List<Institution> findAll() {

		List<Institution> insts = new ArrayList<Institution>();
		Institution inst = null;

		// look in cache first
		Cache cache = HomeFinanceRunner.getCache(CACHE_NAME);
		Map<Object, Element> elementMap = cache.getAll(cache
				.getKeysWithExpiryCheck());
		if (null == elementMap || elementMap.size() == 0) {
			Connection c = null;
			PreparedStatement p = null;
			ResultSet rs = null;

			try {
				c = HomeFinanceRunner.getConnection();

				p = c.prepareStatement("select * from institutions");

				rs = p.executeQuery();

				while (rs.next()) {

					inst = new Institution(rs.getInt(1), rs.getString(2));
					insts.add(inst);
					cache.put(new Element(inst.getId(), inst));
					cache.put(new Element(inst.getName(), inst));
				}

			} catch (SQLException e) {

				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				HomeFinanceRunner.closeConnection(c, p, rs);
			}
		} else {
			for (Iterator<Element> iterator = elementMap.values().iterator(); iterator
					.hasNext();) {

				Element element = iterator.next();

				inst = (Institution) element.getObjectValue();
				insts.add(inst);
			}
		}

		return insts;
	}

	public static Institution getById(final Integer id) {

		Institution ret = null;
		// look in cache first
		Cache cache = HomeFinanceRunner.getCache(CACHE_NAME);
		Element element = cache.get(id);

		if (null == element) {
			Connection c = null;
			PreparedStatement p = null;
			ResultSet rs = null;

			try {
				c = HomeFinanceRunner.getConnection();

				p = c.prepareStatement("select id, name from institutions where id = ?");
				p.setInt(1, id);

				rs = p.executeQuery();

				while (rs.next()) {

					Integer dbId = rs.getInt(1);

					if (dbId.equals(id)) {
						ret = new Institution(dbId, rs.getString(2));
						cache.put(new Element(ret.getId(), ret));
						cache.put(new Element(ret.getName(), ret));
						break;
					}
				}
			} catch (SQLException e) {

				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				HomeFinanceRunner.closeConnection(c, p, rs);
			}
		} else {
			ret = (Institution) element.getObjectValue();
		}

		return ret;

	}

	public final Integer getId() {
		return this.id;
	}

	public final void setId(final Integer id) {
		this.id = id;
	}

	public final String getName() {
		return this.name;
	}

	public final void setName(final String name) {
		this.name = name;
	}

	public final String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
